﻿// --------------------------------
// <copyright file="BackgroundLayer.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:37</date>

// <summary>
//
// </summary>
// --------------------------------
#if WINDOWS

using System.ComponentModel;
using FITClub.Episode1.Levels;
using System.Drawing.Design;

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using FITClub.Util;

namespace FITClub.TextureMenu
{
    public class BackgroundLayer : IDisplayable, IUpdateableItem
    {
        /// <summary>
        /// Path to texture in game content.
        /// </summary>
        [XmlElement]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string TexturePath { get; set; }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        public int MoveCoefficient { get; set; }

        [XmlAttribute]
        public int Order { get; set; }

        /// <summary>
        /// Layer indentifier.
        /// </summary>
        [XmlAttribute]
        public string Name { get; set; }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        public int Direction { get; set; }

        /// <summary>
        /// True to extend vertically.
        /// </summary>
        [XmlAttribute]
        public bool FitToHeight { get; set; }

        /// <summary>
        /// Location and size.
        /// </summary>
        [XmlElement]
        public Rectangle Bounds { get; set; }

        /// <summary>
        /// Background image.
        /// </summary>
        [XmlIgnore]
        public Texture2D Texture { get; private set; }

#if WINDOWS

        [Browsable(false)]
#endif
        [XmlIgnore]
        public bool ContentLoaded { get; set; }

        public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (ContentLoaded == false)
            {
                this.Texture = loader.Load<Texture2D>(this.TexturePath);
                ContentLoaded = true;
                if (this.FitToHeight)
                {
                    Bounds = new Rectangle(0, 0, (int)(this.Texture.Width * MenuManager.ScaleX), (this.FitToHeight) ? MenuManager.Current.GraphicsDevice.Viewport.Height : Texture.Height);
                }
            }
        }

        public void Dispose()
        {
            if (ContentLoaded)
            {
                Texture.Dispose();
                ContentLoaded = false;
            }
        }

        public void Draw(SpriteBatch spriteBatch, Color tint)
        {
            spriteBatch.Draw(this.Texture, Bounds, tint);
        }

        public virtual void Resized(double ratioXdelta, double ratioYdelta)
        {
            if (this.FitToHeight)
            {
                if (Direction > 0)
                {
                    int x = (int)Math.Ceiling(60.0 * (double)MenuManager.Current.GraphicsDevice.Viewport.Width / (double)this.MoveCoefficient);
                    Bounds = new Rectangle(-x, 0, (int)(Bounds.Width * ratioXdelta), MenuManager.Current.GraphicsDevice.Viewport.Height);
                }
                else
                {
                    Bounds = new Rectangle(0, 0, (int)(Bounds.Width * ratioXdelta), MenuManager.Current.GraphicsDevice.Viewport.Height);
                }
            }
            else
            {
                Bounds = Bounds.ScaleXY(ratioXdelta, ratioYdelta);
            }
        }

        public virtual void Move(Microsoft.Xna.Framework.Point delta)
        {
            Bounds.Offset(delta);
        }

        public virtual void Update(Microsoft.Xna.Framework.GameTime gt)
        {
            int d = Direction * MenuManager.Current.GraphicsDevice.Viewport.Width / this.MoveCoefficient;
            Bounds = Bounds.OffsetXY(d);
        }
    }
}